package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entitry.AddressBook;
import com.itheima.reggie.entitry.User;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    @Autowired
    UserService userService;

    /**
     *
     * 新增地址数据
     * */
    @PostMapping
    public R<String> insertAddress(@RequestBody AddressBook addressBook, HttpSession session){
        //通过session来确定当前用户
        User user = (User) session.getAttribute("user");
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,user.getPhone());
        User currentUser = userService.getOne(lqw);

        //设定userid
        addressBook.setUserId(currentUser.getId());

        //新增地址
        addressBookService.save(addressBook);
        return R.success("保存地址成功！");
    }


    /**
     *
     * 设置默认地址
     *
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){

        //设置该对象的is_defaulty属性
        addressBook.setIsDefault(1);
        //通过地址id找当对应的userID
        AddressBook byId = addressBookService.getById(addressBook.getId());
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,byId.getUserId());

        //创建用于将其他地址设为非默认地址的功能对象
        AddressBook addressBook1 = new AddressBook();
        addressBook1.setIsDefault(0);
        //将此userid下的所有地址设为0（非默认）
        addressBookService.update(addressBook1,lqw);
        //将当前的addressBook对象设为默认地址
        addressBookService.updateById(addressBook);

        return R.success("设置成功！");
    }

    /*
    * 根据ID查询地址
    *
    * */

    @GetMapping("/{id}")
    public R<AddressBook> selectByAddressId(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /*
    * 查询默认地址
    *
    * */
    @GetMapping("/default")
    public R<AddressBook> selectDefault(HttpSession session){
        //通过session获取当前user
        User user = (User) session.getAttribute("user");
        //编写条件构建器
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,user.getId()).eq(AddressBook::getIsDefault,1);

        //找到默认地址
        AddressBook addressBook = addressBookService.getOne(lqw);
        return R.success(addressBook);

    }

    /**
     *
     * 查询用户的全部地址
     */

    @GetMapping("/list")
    public R<List<AddressBook>> selectByUser(HttpSession session){

        //获取当前用户id
        User user = (User) session.getAttribute("user");

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,user.getId());


        List<AddressBook> list = addressBookService.list(lqw);

        return R.success(list);
    }

    /**
     *
     * 删除地址
     *
     */

    @DeleteMapping
    public R<String> deleteIds(Long[] ids){
        //删除地址
        for (Long id : ids) {
            addressBookService.removeById(id);
        }

        return R.success("删除成功@！");
    }

    /**
     *
     * 修改地址
     */

    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

}
